const Router = require("koa-Router");
const router = new Router();
const OSS = require("ali-oss");
const { AlipaySdk } = require("alipay-sdk");

// 医院列表
const nosocomium = require("./controller/nosocomium");
router.get("/nosocomium/list", nosocomium.getNosocomiumList);
// 医院详情
router.get("/nosocomium/detail", nosocomium.getNosocomiumDetail);
// 医院新增
router.put("/nosocomium/list", nosocomium.addNosocomium);
// 医院修改
router.post("/nosocomium/list", nosocomium.updateNosocomium);
// 医院删除
router.delete("/nosocomium/list", nosocomium.deleteNosocomium);

// 文章列表
router.get("/book/list", nosocomium.getBookList);
// 获取文章详情
router.get("/book/detail", nosocomium.getBooksDetail);
// 文章阅读量
router.post("/book/read", nosocomium.bookRead);
// 删除文章
router.delete("/book/list", nosocomium.deleteBook);
// 文章搜索
router.get("/book/search", nosocomium.getBookSearch);
// 新增文章
router.put("/book/list", nosocomium.addBook);
// 修改文章
router.post("/book/list", nosocomium.updateBook);

// 医生列表
router.get("/doctor/list", nosocomium.getDoctor);
// 医生详情
router.get("/doctor/detail", nosocomium.getDoctorDetail);
// 关注医生
router.post("/doctor/flag", nosocomium.attentionDoctor);
// 获取关注医生
router.get("/doctor/flag", nosocomium.getDoctorlistLike);

// 后台rbac
const doctor = require("./controller/doctor");
// 浏览量
router.post("/doctor/browse", doctor.doctorListBrowse);
// 删除医生
router.delete("/doctor/list", doctor.doctorListDelet);
// 新增医生
router.put("/doctor/list", doctor.doctorListAdd);
// 修改医生
router.post("/doctor/list", doctor.doctorListEdit);
// 服务过我的医生
router.get("/doctor/service", doctor.doctorListService);
// 新增服务过我的医生
router.put("/doctor/service", doctor.doctorListServiceAdd);

const addres = require("./controller/addres");
// 新增收货地址
router.put("/address/add", addres.AddAddres);
// 获取收货地址
router.get("/address/list", addres.getAddres);
// 修改收货地址
router.post("/address/update", addres.updateAddres);
// 删除收货地址
router.delete("/address/delete", addres.AddresDelete);
// 修改默认地址
router.post("/address/default", addres.setDefaultAddress);

// 获取科室数据
const office = require("./controller/office");
router.get("/office/list", office.getOffice);

// 获取药品数据
const restorative = require("./controller/restorative");
router.get("/restorative/list", restorative.getRestorativeList);
// 获取药品条件
router.get("/restorative/type", restorative.getRestorativeType);
// 获取药品详情
router.get("/restorative/detail", restorative.getRestorativeDetail);
// 删除药品
router.delete("/restorative/list", restorative.deleteRestorative);
// 新增药品
router.put("/restorative/list", restorative.addRestorative);
// 编辑药品
router.post("/restorative/list", restorative.editRestorative);

// 搜索
const search = require("./controller/search");
router.get("/search", search.searchList);
// 搜索科室
router.get("/search/office", search.searchAdmin);

// 个人信息
const user = require("./controller/user");
// 获取
router.get("/user/list", user.getUserList);
// 新增
router.put("/user/list", user.userListAdd);
// 删除
router.delete("/user/list", user.userListDel);

// 评论
const review = require("./controller/review");
// 获取评论条件
router.get("/review/classify", review.getClassify);
// 获取评论列表
router.get("/review/list", review.getReviewList);
// 新增评论列表
router.put("/review/list", review.addReview);
// 获取锦旗
router.get("/review/banner", review.getBanner);
// 获取历史评论记录
router.get("/review/banview", review.getBanview);
// 赠送锦旗
router.post("/review/banview", review.giveBanner);
// 获取视频
router.get("/review/vidou", review.getVidou);

// 上传图片
const client = new OSS({
  // yourregion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
  region: "oss-cn-beijing",
  // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: "LTAI5tESh6fhXn6phqN9VGnB",
  accessKeySecret: "FveyKNSsmq9TpwTo6LzHW2WLcCjsEb",
  // 填写Bucket名称。
  bucket: "zifan1998",
});
router.post("/upload", async (ctx) => {
  let file = ctx.request.files.file;
  try {
    // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
    // 如果本地文件的完整路径中未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
    const result = await client.put(file.originalFilename, file.filepath);
    ctx.body = {
      code: 200,
      msg: "上传成功",
      url: result.url,
    };
  } catch (e) {
    ctx.body = {
      code: 401,
      msg: "上传失败",
    };
    console.log(e);
  }
});

// 支付
const alipaySdk = new AlipaySdk({
  appId: "9021000139665423",
  // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致，参考平台配置一节
  privateKey:
    "MIIEowIBAAKCAQEAsRIdLBNzGSanMtJ3HtF5gXDnLsg8HBNw8MNILy5SJexi6kqDcHz/deCsPmOfAl5OOHryKhTyJUNuZ/wJrh34q6FHZj7tBX1WshPRbZ8rB0l2Fs5DErH0yDHd4RQO/zVvufrLoUJq3rqx3H8krbNj0QMC7luggrfAUqzIgzaOmXBu7nIh2xmDf0pEftvFuCab91o4XyrtvZG4UumiqMws2f7fHulSbuaJF9RralYAJjvXTSjY3I3k8uOUaamvRmHUHAJJhbO5N2L+cjB5zp92apP15x30DeMOHbM7h0gcw/f406i7UrSG6lRv07FDtAKEgmF4RmQYP+1tLciaNJJd/wIDAQABAoIBACvUTgBA+DZ7iNtdmTq5oxfVOIQt8CMK9OPDkrSLBK56aqmPtCU964XkDP6MbimJ6la0qAiekqmCnWuDyWmd5ZWmICRA4y5+rdz6ZtZ5lY8CMgbHG3cfMXX0UfWKn4OwWG1ylMDfzdA/6hKoLg4I32Y3FpyldX+luCc+uYsxD+8OqMXiqbpco4vHllvoi7GoPzb1M05Wq0G+s1tv2zPrSQB7oeP5arfd6KTvBxese85BPOBrW1ly/5puXJ7wiQsbaU/qbu4gMMZOOW5uMHxWrbsJ7na9fOGpPU0bsr9F+PkbvSfvbitPJcDCCKwXgG9OztKRVvI4O6EtyUijiZPpsBECgYEA+dhI+v+eBnyjj1edKOJ7s2ZRmDltQQDntgC0kBbnbaqsRwa3yMWlXxKKZHrFJKwatk4KRjyIqaYAI4cgOFXTHx6nNPE2KPI5ooRTTcS5IVeXMc1TfIgkHc+0MOEZiB6DP++kAV5YKQ2ddQQh7ITPbSOvhald4sp/yPXwe3Q8xtsCgYEAtW7b7Oy9tP6wJHsPJkjGgvL+7o4N0TdYuCXoFSFFuPiFVw0gSz4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aydev.com/gateway.do",
});

router.get("/pay", async (ctx) => {
  let { id, price, title, body } = ctx.query;

  const bizContent = {
    out_trade_no: id,
    product_code: "FAST_INSTANT_TRADE_PAY",
    subject: title,
    body: body,
    total_amount: price,
  };

  // 支付页面接口，返回 html 代码片段，内容为 Form 表单
  const result = alipaySdk.pageExec("alipay.trade.wap.pay", {
    method: "GET",
    bizContent,
    returnUrl: "https://www.taobao.com",
  });
  ctx.body = {
    code: 200,
    msg: "支付中",
    result: result,
  };
});

// 挂号添加
const order = require("./controller/order");
router.put("/order", order.orderadd);
// 挂号列表
router.get("/order", order.orderlist);
// 挂号详情
router.get("/orderdetail", order.orderdetail);

// 发送验证码
const login = require("./controller/login");
router.post("/login", login.getPhoneCode);
// 判断验证码是否正确
router.get("/login", login.checkPhoneCode);

module.exports = router;
